﻿using Data;
using DevExpress.XtraBars;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using FromUtils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RequestDirectory
{
    static class PopupMenuControl
    {
        /// <summary>
        /// 鼠标右键-菜单显示界面逻辑
        /// </summary>
        public static void FormMenuControl(object sender, 
            MouseEventArgs e, PopupMenu pm, 
            Point MousePosition, ref TreeListNode currentNode)
        {

            TreeListNode tn = TreeListTool.GetFocusNode(sender, e.Location);
            //右键显示 菜单
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                currentNode = tn;
                PopupMenu_Initialize(pm);
                if (tn != null)
                {
                    //设置焦点
                    ((TreeList)sender).SetFocusedNode(tn);
                    string stringtype = (string)tn.GetValue("type");
                    if (stringtype.Equals("file"))
                    {
                        PopupMenu_Open_file(pm);
                    }
                    else
                    {
                        PopupMenu_Open_filegroup(pm);
                    }
                }
                else
                {
                    //git显示git菜单
                    if (GlobalDirectory.GitRemote)
                    {
                        PopupMenu_Add(pm,true);
                    }
                    else {
                        PopupMenu_Add(pm);
                    }
                   
                }
                pm.ShowPopup(MousePosition);
            }
        }

        /// <summary>
        /// 菜单初始化
        /// </summary>
        /// <param name="pm"></param>
        private static void PopupMenu_Initialize(PopupMenu pm)
        {
            pm.ItemLinks[0].Visible = true;
            pm.ItemLinks[1].Visible = true;
            pm.ItemLinks[1].Caption = "展开关闭";
            pm.ItemLinks[2].Visible = true;
            pm.ItemLinks[3].Visible = true;
        }

        /// <summary>
        /// 菜单-添加文件夹与请求器
        /// </summary>
        /// <param name="pm"></param>
        private static void PopupMenu_Add(PopupMenu pm,Boolean git = false)
        {
            pm.ItemLinks[1].Visible = false;
            pm.ItemLinks[2].Visible = false;
            pm.ItemLinks[3].Visible = false;
            pm.ItemLinks[4].Visible = false;
            pm.ItemLinks[5].Visible = false;
            if (git) {
                pm.ItemLinks[6].Visible = true;
            }
            else
            {
                pm.ItemLinks[6].Visible = false;
            }            
        }

        /// <summary>
        /// 菜单-请求器上右键
        /// </summary>
        /// <param name="pm"></param>
        private static void PopupMenu_Open_file(PopupMenu pm)
        {
            pm.ItemLinks[0].Visible = false;
            pm.ItemLinks[1].Visible = true;
            pm.ItemLinks[1].Caption = "打开";
            pm.ItemLinks[2].Visible = true;
            pm.ItemLinks[3].Visible = true;
            pm.ItemLinks[4].Visible = true;
            ((BarSubItem)pm.ItemLinks[4].Item).ItemLinks[0].Visible = true;
            ((BarSubItem)pm.ItemLinks[4].Item).ItemLinks[1].Visible = false;
            pm.ItemLinks[5].Visible = true;
            pm.ItemLinks[6].Visible = false;
        }

        /// <summary>
        /// 菜单-文件夹上右键
        /// </summary>
        /// <param name="pm"></param>
        private static void PopupMenu_Open_filegroup(PopupMenu pm)
        {
            pm.ItemLinks[0].Visible = true;
            pm.ItemLinks[1].Visible = false;
            pm.ItemLinks[2].Visible = true;
            pm.ItemLinks[3].Visible = true;
            pm.ItemLinks[4].Visible = true;
            ((BarSubItem)pm.ItemLinks[4].Item).ItemLinks[0].Visible = true;
            ((BarSubItem)pm.ItemLinks[4].Item).ItemLinks[1].Visible = true;
            pm.ItemLinks[5].Visible = true;
            pm.ItemLinks[6].Visible = false;
        }
    }

}
